package com.netty.server;


import com.nettryhomework.RpcRequest;
import com.nettryhomework.UserService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


public class UserServerHandler extends ChannelInboundHandlerAdapter {

    private UserService userService;

    public UserServerHandler(UserService userService) {
        this.userService = userService;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("开始处理");
        RpcRequest rpcRequest = (RpcRequest) msg;
        String serviceClass = rpcRequest.getServiceClass();
        String method = rpcRequest.getMethod();
        Object[] parameters = rpcRequest.getParameters();
        Class<?>[] parameterTypes = rpcRequest.getParameterTypes();

        Class<?> aClass = Class.forName(serviceClass);

        Method method1 = aClass.getMethod(method,parameterTypes);
        method1.invoke(userService,parameters);

        ctx.writeAndFlush("success");

    }
}
